package com.threeti.jiayusms.infrastructure.defaulttemplate;

import static org.apache.commons.lang.StringUtils.isNotEmpty;
import static org.hibernate.criterion.Restrictions.disjunction;
import static org.hibernate.criterion.Restrictions.eq;
import static org.hibernate.criterion.Restrictions.like;
import static org.springframework.util.CollectionUtils.isEmpty;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Junction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.threeti.jiayusms.domain.model.defaulttemplate.DefaultCommunicationTemplate;
import com.threeti.jiayusms.framework.dao.AbstractCustomKeyDao;
import com.threeti.jiayusms.framework.util.DynaSearchCriteria;
import com.threeti.jiayusms.framework.util.PagedListHandler;
import com.threeti.jiayusms.interfaces.DefaultTemplateDynaCriteriaDto;

@Repository
public class DefaultCommunicationTemplateDaoImpl extends AbstractCustomKeyDao<String, DefaultCommunicationTemplate> implements DefaultCommunicationTemplateDao {
  @Autowired
  public DefaultCommunicationTemplateDaoImpl(SessionFactory sessionFactory) {
    super(sessionFactory);
  }

  @Override
  protected DetachedCriteria buildDefaultCriteria() {
    return DetachedCriteria.forClass(DefaultCommunicationTemplate.class);
  }

  @Override
  public DefaultCommunicationTemplate findDefaultEnabledTemplate() {
    DetachedCriteria criteria = buildDefaultCriteria();
    criteria.add(eq("enabled", true));

    List<DefaultCommunicationTemplate>  templates = this.findAllBy(criteria);
    if (!isEmpty(templates))  return templates.get(0);

    return null;
  }

  @Override
  public PagedListHandler<DefaultCommunicationTemplate> findAllDefaultTemplatesByPage(int currentPageIndex,
                                                                                      int startingIndex) {
    PagedListHandler<DefaultCommunicationTemplate> pagedListHandler = this.findAllByPage(currentPageIndex, startingIndex);
    List<DefaultCommunicationTemplate> pagedList = pagedListHandler.getPageList();
    if (!isEmpty(pagedList))  return pagedListHandler;

    return PagedListHandler.EMPTY_LIST;
  }

  @Override
  public PagedListHandler<DefaultCommunicationTemplate> findDefaultTemplatesByPage(DynaSearchCriteria dynaSearchCriteria,
                                                                                   int currentPageIndex,
                                                                                   int startingIndex) {
    DefaultTemplateDynaCriteriaDto dynaCriteria = (DefaultTemplateDynaCriteriaDto)dynaSearchCriteria;
    String templateIdKeywords = dynaCriteria.getTemplateIdKeywords();
    String templateDescriptionKeywords = dynaCriteria.getTemplateDescriptionKeywords();
    String enabled = dynaCriteria.getDefaultEnable();

    //模糊查询
    DetachedCriteria criteria = buildDefaultCriteria();
    Junction orClause = disjunction();    
    //模板编号    
    try {
      if (isNotEmpty(templateIdKeywords)) {
        String[] idKeywords = templateIdKeywords.split(" ");
        for (int i = 0; i < idKeywords.length; i++) {
          String encodeIdKeyWord = new String(idKeywords[i].getBytes("iso8859-1"), "utf-8");
          String idKeyword = "%" + encodeIdKeyWord + "%";
          orClause.add(like("this.customId", idKeyword).ignoreCase());
        }
      }
      //模板描述
      if (isNotEmpty(templateDescriptionKeywords)) {
        String[] descKeywords = templateDescriptionKeywords.split(" ");
        for (int i = 0; i < descKeywords.length; i++) {
          String encodeDescKeyWord = new String(descKeywords[i].getBytes("iso8859-1"), "utf-8");
          String descKeyword = "%" + descKeywords[i] + "%";
          orClause.add(like("this.description", descKeyword).ignoreCase());
        }
      }
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    criteria.add(orClause);
    
    if (isNotEmpty(enabled)) {
      criteria.add(eq("this.enabled", "1".equals(enabled)?true:false));
    }

    PagedListHandler<DefaultCommunicationTemplate>  pagedListHandler = this.findByPage(criteria, currentPageIndex, startingIndex);
    List<DefaultCommunicationTemplate> pagedList = pagedListHandler.getPageList();
    if (!isEmpty(pagedList))  return pagedListHandler;

    return PagedListHandler.EMPTY_LIST;
  }
  
}
